/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Mercado.view;

import Mercado.model.Cadastro;
import Mercado.model.Estoque;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

/**
 *
 * @author rafael
 */
public class Janela {
    
    private JFrame  janela;
    Venda_Panel     pVenda;
    Estoque_Panel   pEstoque;
    Clientes_Panel  pClientes;
    Relatorio_Panel pRelatorios;
    JTabbedPane     abas;
    private JMenu   menu,menu2;
    private Cadastro cad;
    private Estoque e;
    public int      lastActive = 0;
    
    
    public Janela(Estoque e, Cadastro cad){
        this.e      = e;
        this.cad    = cad;
        janela      = new JFrame("Armazém");
        pClientes   = new Clientes_Panel(cad);
        pEstoque    = new Estoque_Panel(e);
        pVenda      = new Venda_Panel(cad, e);
        abas        = new JTabbedPane(JTabbedPane.TOP);
        pRelatorios = new Relatorio_Panel(cad,e);
        
        pClientes.  addComponentListener(new evtAba());
        pEstoque.   addComponentListener(new evtAba());
        pVenda.     addComponentListener(new evtAba());        
        pRelatorios.     addComponentListener(new evtAba());        
        
        abas.addTab("Clientes",pClientes);
        abas.addTab("Venda",pVenda);
        abas.addTab("Estoque",pEstoque);
        abas.addTab("Relatórios",pRelatorios);
        janela.setSize(640,480);
        
        janela.add(abas);
        //janela.add(pClientes);
                
        janela.addWindowListener(new WindowListener() {

            @Override
            public void windowOpened(WindowEvent e) {}
            @Override
            public void windowClosing(WindowEvent e) {
                if (lastActive==1)
                    setCad(pVenda.getCad());
                else if(lastActive==1){
                    setCad(pVenda.getCad());
                    setE(pVenda.getE());
                }
                else if(lastActive==2)
                    setE(pEstoque.getE());
                getE().salvaEstoque();
                getCad().salvaCadastro();
            }
            

            @Override
            public void windowClosed(WindowEvent e) {
            }
            
            @Override
            public void windowIconified(WindowEvent e) {}
            @Override
            public void windowDeiconified(WindowEvent e) {}
            @Override
            public void windowActivated(WindowEvent e) {}

            @Override
            public void windowDeactivated(WindowEvent e) {}
        });
        
        janela.setLocationRelativeTo(null);
        janela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        janela.setVisible(true);
    }

    public Cadastro getCad() {
        return cad;
    }

    public Estoque getE() {
        return e;
    }

    public void setCad(Cadastro cad) {
        this.cad = cad;
    }

    public void setE(Estoque e) {
        this.e = e;
    }

    private class evtAba implements ComponentListener {

        @Override
        public void componentShown(ComponentEvent evt) {
            if(evt.getComponent().equals(pClientes)){
                if (lastActive==1){
                    setCad(pVenda.getCad());
                    pClientes.setCad(getCad());
                }
                lastActive=0;
            }
            else if (evt.getComponent().equals(pVenda)){
                if(lastActive==0){
                    setCad(pClientes.getCad());
                    pVenda.setCad(getCad());
                }
                else if(lastActive==2){
                    setE(pEstoque.getE());
                    pVenda.setE(getE());
                }
                lastActive=1;
            }
            else if(evt.getComponent().equals(pEstoque)){
                if(lastActive==1){
                    setE(pVenda.getE());
                    pEstoque.setE(getE());
                }
                pEstoque.controller.atualizaTabela(pEstoque.listaItens);
                lastActive=2;
            }
            else if(evt.getComponent().equals(pRelatorios)){
                if(lastActive==0){
                    setCad(pClientes.getCad());
                    pRelatorios.setCad(getCad());
                }
                else if(lastActive==1){
                    setCad(pVenda.getCad());
                    pRelatorios.setCad(getCad());
                    setE(pVenda.getE());
                    pRelatorios.setE(getE());
                }
                else if(lastActive==2){
                    setE(pEstoque.getE());
                    pRelatorios.setE(getE());
                }
            }
        }

        @Override
        public void componentHidden(ComponentEvent e) {}
        @Override
        public void componentResized(ComponentEvent e) {}
        @Override
        public void componentMoved(ComponentEvent e) {}
    }
    
}